<?php
namespace app\index\service;
use think\Session;
use think\Validate;
use app\model\Goods;
use app\model\Seller;
use app\model\Order;
use app\model\Charge;

class SellerService{
    public function balance($param)
    {
        if(!array_key_exists('status',$param)){
            $param['status'] = 1;
        }
        if($param['status'] == 1){
            $str = 'status in (0,1)';
        }else{
            $str = 'status = 2';
        }
        $user = Session::get('date');
        $goodsModel = new Goods;
        $goods_ids = $goodsModel::where('uid',$user['id'])->column('sp_id');
        $orderModel = new Order;
        $orders = $orderModel::with('goods')->where('num_iid','in',implode(',',$goods_ids))->where($str)->order('create_time desc')->paginate(20,false,[
            'query' => $param,
            'type'      => 'page\Page',
            'var_page'  => 'page',
        ]);
        // dump($orders);die;
        $page = $orders->render();
        return ['orders'=>$orders,'page'=>$page];
    }

    public function makePost($post)
    {
        $rule = [
            'account'  => 'require|number',
            'picture'  => 'require',
        ];
        $msg = [
            'account.require'  => '打款金额必须',
            'account.number'   => '请填写正确的打款金额',
            'picture.require'  => '请上传打款截图',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($post)){
            return ['code'=>-1,'msg'=>$validate->getError()];
        }
        $user = Session::get('date');
        $sellerModel = new Seller;
        $seller = $sellerModel::where('uid',$user['id'])->find();
        $data = [
            'uid'       => $user['id'],
            'seller_id' => $seller->id,
            'account'   => $post['account'],
            'picture'   => $post['picture'],
            'add_time'  => time(),
        ];
        // dump($data);die;
        $chargeModel = new Charge;
        $info = $chargeModel::insert($data);
        if(!$info){
            return ['code'=>-2,'msg'=>'系统繁忙'];
        }
        return ['code'=>1,'msg'=>'已提交，请等待审核'];
    }
}
